import {useEffect, useState} from "react";
import {Spin, message} from "antd";
import './index.scss'
import Query from "./query";
import AlarmList from "./content";
import {apiDeleteAlarmList, apiGetAlarmList} from "@/common/apis/journal";
import {IPageResponse, IParam, PageInfo} from "@/common/common";

const AbnormalAlarm = () => {

  console.log('-------------AbnormalAlarm--------------')

  const [loading, setLoading] = useState<boolean>(false)
  const [params, setParams] = useState<IParam>({page: 1, size: 10});
  const [pageResponse, setPageResponse] = useState<IPageResponse<any>>({pageInfo: {page: 1, size: 10, total: 0}})

  useEffect(() => {
    getAlarmList()
  }, [params]);

  const getAlarmList = async () => {
    setLoading(true)
    const res = await apiGetAlarmList(params || {});
    if (res.code === 0) {
      setPageResponse(res)
    }
    setLoading(false)
  }

  const queryCallBack = (param: IParam) => {
    setParams({...param, ...{page: params.page, size: params.size}})
  }

  const delRecords = async (ids: number[]) => {
    const res = await apiDeleteAlarmList(ids);
    if (res.code === 0) {
      message.success("删除成功");
      await getAlarmList();
    }
  }

  const handleRecords = async (ids: number[]) => {
    console.log(ids)
  }

  return (
    <div className="abnormalContainer">
      <Spin spinning={loading}>
        <Query onQueryCallBack={queryCallBack}/>
        <AlarmList
          pageResponse={pageResponse}
          onChangePage={(pageInfo: PageInfo) => {
            if (pageInfo.page !== params.page || pageInfo.size !== params.size) {
              setParams({...params, ...pageInfo})
            }
          }}
          delRecord={(ids: number[]) => {
            delRecords(ids)
          }}
          handleRecord={(ids: number[]) => {
            handleRecords(ids)
          }}
        />
      </Spin>
    </div>
  )
}

export default AbnormalAlarm
